package cn.neusoft.luofa603.yuejian_29.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.activity.TrainInfoActivity;
import cn.neusoft.luofa603.yuejian_29.adapter.QuanbuAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Training;
import cn.neusoft.luofa603.yuejian_29.model.TrainModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class QuanbuFragment extends Fragment {

    private View view;
    private String page;
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_quanbu, container, false);
        initViews();
        initEvents();
        initData();
        return view;
    }
    public void initViews(){
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_quanbu);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    public void initEvents(){

    }
    public void initData(){

        TrainModel trainModel = new TrainModel();
        Call<List<Training>> call = trainModel.getAll();
        call.enqueue(new Callback<List<Training>>() {
            @Override
            public void onResponse(Call<List<Training>> call, Response<List<Training>> response) {
                final List<Training> mDataList = response.body();
                QuanbuAdapter quanbuAdapter  = new QuanbuAdapter(getActivity(),mDataList);
                recyclerView.setAdapter(quanbuAdapter);
               quanbuAdapter.setItemClickListener(new QuanbuAdapter.MyItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent();
                        intent.setClass(getActivity(), TrainInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("Training",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });

            }

            @Override
            public void onFailure(Call<List<Training>> call, Throwable t) {
                Toast.makeText(getActivity(),"与服务器通讯失败！",Toast.LENGTH_SHORT).show();

            }
        });

    }


}
